<template>
    <header class="header">
        <div class="header-l">
            <div class="logo" @click="router.push('/home')">
                <img src="/images/logo.png" title="返回首页">
            </div>
        </div>
        <div class="header-r">
            <div class="avatar">
                <img :src="Util.getImgUrl(user.avatar)" ref="avatarRef" class="avatar"
                    @mouseenter="event => showUserMenu(event, true)" @mouseleave="event => showUserMenu(event, false)">
                <div class="user-menu" ref="userMenuRef" @mouseenter="showMenu = true"
                    @mouseleave="event => { showMenu = false; showUserMenu(event, false) }">
                    <div class="nickname">{{ user?.nickname || user?.username }}</div>
                    <div class="user-mannager" @click="toUserOrHome">
                        <div>
                            <el-icon class="user-icon">
                                <UserFilled />
                            </el-icon>
                            <span style="vertical-align: middle;">{{ tabText }}</span>
                        </div>
                        <el-icon>
                            <ArrowRight />
                        </el-icon>
                    </div>
                    <div class="logout" @click="logout()">
                        <span class="iconfont icon icon-tuichu" style="margin-right: 8px;"></span>
                        退出登录
                    </div>
                </div>
            </div>
        </div>
    </header>
    <main class="main">
        <router-view></router-view>
    </main>
    <ChatController />
    <GameInviteBar />
</template>

<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { UserFilled, ArrowRight } from '@element-plus/icons-vue'
import { socketlistenEvent } from '@/socket/socketEventHandler'
import axios from 'axios';
import { Util } from '@/Util'
import { useUserStore } from '@/store'
import { storeToRefs } from 'pinia'
const User = useUserStore()
let { user } = storeToRefs(User);
const router = useRouter()
const route = useRoute()
const userMenuRef = ref()
const avatarRef = ref()
let showMenu: boolean;
let timer: any;
let flag: boolean = false

onMounted(() => {
    getUserInfo()
    socketlistenEvent()
})

async function getUserInfo() {
    const token = localStorage.getItem('token')
    if (token) {
        let { data } = await axios.get('/user', {
            headers: { token }
        })
        if (data.code == '0000') {
            User.setUser(data.data)
        }
    }
}
const tabText = computed(() => {
    return route.path === '/home/gamelist' ? '个人中心' : '回到首页';
})
const toUserOrHome = () => {
    router.push(route.path === '/home/gamelist' ? 'user' : '/home')
}
const logout = () => {
    localStorage.removeItem('token')
    router.push('/login')
}
const showUserMenu = (event: MouseEvent, show: boolean) => {
    let avatar = avatarRef.value
    let userMenu = userMenuRef.value
    if ((!flag || show)) {
        if (show) {
            userMenu.style.transform = 'translateY(0)'
        } else {
            flag = true
            timer = setTimeout(() => {
                if (!showMenu || show) {
                    userMenu.style.transform = 'translateY(-800px)'
                    avatar.style.transform = `scale(1)`
                }
                flag = false
                clearTimeout(timer)
            }, 300)
        }
        let time = setTimeout(() => {
            if (!showMenu || show) {
                userMenu.style.opacity = Number(show)
                avatar.style.transform = `scale(${show ? 2 : 1})`
            }
            clearTimeout(time)
        }, 10)
    }

}

</script>


<style scoped lang="less">
.header {
    width: 100vw;
    height: 60px;
    position: fixed;
    top: 0;
    left: 0;
    background-color: #fff;
    box-shadow: 2px 4px 5px rgba(0, 0, 0, 0.1);
    padding: 10px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-family: 'PingFang SC';
    z-index: 1000;
    background-image: linear-gradient(-225deg, #69EACB 0%, #EACCF8 48%, #6654F1 100%);

    .header-r,
    .header-l {
        display: flex;
        align-items: center;
    }

    .header-l {
        display: flex;
        align-items: center;
        width: 50px;
        height: 50px;
        cursor: pointer;

        img {
            width: 100%;
            height: 100%;
        }
    }

    .header-r {
        .avatar {
            height: 50px;
            width: 50px;
            margin-right: 100px;
            position: relative;
            border-radius: 50%;
            z-index: 1000;

            img {
                position: absolute;
                width: 100%;
                height: 100%;
                transition: .3s;
                transform-origin: right top;
                border-radius: 50%;
                border: 2px solid #fff;
            }

            .user-menu {
                position: absolute;
                top: 58px;
                padding: 55px 30px 0;
                right: -100px;
                width: 300px;
                height: 225px;
                display: flex;
                flex-direction: column;
                align-items: center;
                transition: opacity .3s;
                border-radius: 4px;
                transform: translateY(-800px);
                opacity: 0;
                background-color: #fff;
                border: 1px solid #eee;
                box-shadow: 1px 1px 4px 2px rgba(0, 0, 0, 0.1);

                .nickname {
                    font-size: 20px;
                    font-weight: 500;
                }

                .user-mannager,
                .logout {
                    margin-top: 10px;
                    padding: 0 5px;
                    width: 100%;
                    height: 50px;
                    display: flex;
                    font-weight: 500;
                    align-items: center;
                    border-radius: 5px;
                    transition: background-color .1s;

                    &:hover {
                        background-color: #e3e5e7;
                    }
                }

                .user-mannager {
                    display: flex;
                    justify-content: space-between;

                    .user-icon {
                        margin-right: 9px;
                        color: #000;
                        vertical-align: middle;
                    }
                }

                .logout {
                    .icon {
                        width: 1em;
                        height: 1em;
                        font-size: 20px;
                        vertical-align: -0.15em;
                        fill: currentColor;
                        overflow: hidden;
                    }
                }
            }
        }
    }
}

.main {
    width: 75vw;
    height: 100vh;
    margin: 0 auto;
    padding: 70px 20px 0;
    display: flex;
}
</style>@/socket/socketEventHandler